package cn.study.hashMapTest.bitmodel;

/**
 * @Author     ：跟大佳一起学架构
 * @Date       ：Created in 21:20 2024/5/24
 * @Description：位运算来操作权限控制  性能更高
 */
public class PermissionControl {
    // 权限常量
    public static final int READ = 1; // 0001  读
    public static final int WRITE = 2; // 0010  写
    public static final int EXECUTE = 4; // 0100  执行
    public static final int DELETE = 8; // 1000  删除

    public static void main(String[] args) {
        int permission = 0; // 初始无任何权限  0000

        // 添加权限
        permission |= READ;
        permission |= WRITE;

        // 检查权限
        System.out.println("Can Read: " + ((permission & READ) > 0));
        System.out.println("Can Write: " + ((permission & WRITE) > 0));
        System.out.println("Can Execute: " + ((permission & EXECUTE) > 0));
        System.out.println("Can Delete: " + ((permission & DELETE) > 0));

        // 移除权限(非、与)
        permission &= ~WRITE;

        // 再次检查权限
        System.out.println("Can Write after removal: " + ((permission & WRITE) > 0));
    }
}